package models

import (
	"strings"
	"github.com/ylywyn/jpush-api-go-client"
		goerrors "github.com/go-errors/errors"
)



func PushMsg2PostmanAsync(qid string, extras map[string]interface{}) {
	go func() {
		if err := PushMsg([]string{qid}, secret_postman, appKey_postman, "", extras); err != nil {
			Log.Alert("push to %s %v failed,beacause %v", qid, extras, err)
		}
		}()
}

func PushMsg2MasterAsync(qid string, extras map[string]interface{}) {
	go func() {
		if err := PushMsg([]string{qid}, secret, appKey, "", extras); err != nil {
						Log.Alert("push to %s %v failed,beacause %v", qid, extras, err)
		}
		}()
}

func PushMsg(ids []string, secret, appKey, content string, extras map[string]interface{}) error {
	//Platform
	var pf jpushclient.Platform
	pf.All()

	//Audience
	var ad jpushclient.Audience
	ad.SetAlias(ids)

	// Msg
	var msg jpushclient.Message
	msg.Content = content
	msg.Extras = extras

	payload := jpushclient.NewPushPayLoad()
	payload.SetPlatform(&pf)
	payload.SetAudience(&ad)
	payload.SetMessage(&msg)
	//payload.SetNotice(&notice)
	//payload.SetOptions(&jpushclient.Option{TimeLive: 864000})

	bytes, _ := payload.ToBytes()

	//push
	c := jpushclient.NewPushClient(secret, appKey)
	rst, err := c.Send(bytes)
	if err != nil {
		return goerrors.New(err)
	} else {
		if strings.Contains(rst, "error") {
			return goerrors.New(rst)
		}
	}
	return nil
}


